﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model;
using Newtonsoft.Json.Linq;
using ZBZH_Manage_Order.Models;
using ZBZH_Manage_Order.PublicMethod;

namespace ZBZH_Manage_Order.Controllers.SystemSettings
{
    public class SystemSetController : BaseController
    {
        public ActionResult Index()
        {
          
            return View();
        }

        #region 获取系统设置信息
        /// <summary>
        /// 获取系统设置信息
        /// </summary>
        /// <returns></returns>
        public ActionResult GetOrder_Key()
        {
            try
            {
                string result = SRS.GetOrder_Key(YZM, 1);
                List<Model.Order_Key_T> List_Info = new List<Order_Key_T>();
                JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                string bs = searcho["BS"].ToString();
                //bs.Substring(0,1)
                if (bs == "1")
                {
                    IList<JToken> jieguo = searcho["rows"].Children().ToList();//解析返回的结果
                    if (jieguo.Count > 0)
                    {
                        List_Info = ParseClass<Model.Order_Key_T>.JsonList(jieguo);//解析返回的结果
                        //foreach (JToken item in jieguo)
                        //{
                        //    Model.Order_Key_T m = new Model.Order_Key_T();
                        //    m.ComOpen = Int32.Parse(item["ComOpen"].ToString());
                        //    m.ID = Int32.Parse(item["ID"].ToString());
                        //    m.IntOpenPT = Int32.Parse(item["IntOpenPT"].ToString());
                        //    m.IntOpenSH = Int32.Parse(item["IntOpenSH"].ToString());
                        //    m.IntShOpen = Int32.Parse(item["IntShOpen"].ToString());
                        //    m.TopMer = item["TopMer"].ToString();
                        //    List_Info.Add(m);
                        //}
                    }

                    return Json(List_Info, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    return Content(searcho["Msg"].ToString());
                }
            }
            catch (Exception)
            {
                return null;
            }

        }
        #endregion

        #region 普通排序切换到自定义排序
        /// <summary>
        /// 获取商户排位信息
        /// </summary>
        /// <returns></returns>
        public ActionResult mysort(JqGridParam jqgridparam)
        {
            try
            {
                List<Model.View_Order_MerSet> List = new List<Model.View_Order_MerSet>();
                Page<Model.View_Order_MerSet> page = new Page<Model.View_Order_MerSet>();
                //List<Model.View_Order_MerSet> List1 = new List<View_Order_MerSet>();//用于保存有排序的商户
                //List<Model.View_Order_MerSet> List2 = new List<View_Order_MerSet>();//用于保存新增的未进行排序的商户

                string result = SRS.GetOrder_Mer(YZM, 1);
                JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                string bs = searcho["BS"].ToString();//获取返回结果是否正常标识
                if (bs == "1")//返回结果正常
                {
                    IList<JToken> jieguo = searcho["rows"].Children().ToList();//解析返回的结果
                    List = ParseClass<Model.View_Order_MerSet>.JsonList(jieguo);//解析返回的结果
                    jqgridparam.records = List.Count;
                    var data = new
                    {
                        total = searcho["total"].ToString(),
                        rows = page.FindListPage(List, ref jqgridparam)//返回当前页的数据
                    };
                    return Json(data);
                }
                else
                {
                    var data = new
                    {
                        total = searcho["total"].ToString(),
                        rows = List
                    };
                    return Json(data);
                }
            }
            catch (Exception)
            {
                return null;
            }

        }
        #endregion

        #region 获取商户排位信息
        /// <summary>
        /// 获取商户排位信息
        /// </summary>
        /// <returns></returns>
        public ActionResult getgrid(string TopMer, JqGridParam jqgridparam)
        {
            try
            {
                List<Model.View_Order_MerSet> List = new List<Model.View_Order_MerSet>();

                List<Model.View_Order_MerSet> List1 = new List<View_Order_MerSet>();//用于保存有排序的商户
                List<Model.View_Order_MerSet> List2 = new List<View_Order_MerSet>();//用于保存新增的未进行排序的商户

                string result = SRS.GetOrder_Mer(YZM, 1);
                JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                string bs = searcho["BS"].ToString();//获取返回结果是否正常标识
                if (bs == "1")//返回结果正常
                {
                    IList<JToken> jieguo = searcho["rows"].Children().ToList();//解析返回的结果
                    List = ParseClass<Model.View_Order_MerSet>.JsonList(jieguo);//解析返回的结果

                    if (TopMer.Length > 2)
                    {
                        TopMer = TopMer.Substring(2, TopMer.Length - 2);
                        string[] toplist = TopMer.Split(',');

                        //此处要对自定义排序的商户做排序显示，代码未完成
                        foreach (var item in toplist)
                        {
                            List1.Add(List.Where(u => u.MerID == item).First());
                        }
                        List2 = (from m in List where !(toplist.Contains(m.MerID)) select m).ToList();
                        List1.AddRange(List2);
                    }

                    var data = new
                    {
                        total = searcho["total"].ToString(),
                        rows = List1//返回当前页的数据
                    };
                    return Json(data);
                }
                else
                {
                    var data = new
                    {
                        total = searcho["total"].ToString(),
                        rows = List
                    };
                    return Json(data);
                }
            }
            catch (Exception)
            {
                return null;
            }

        }
        #endregion

        #region 获取当前页数据
        /// <summary>
        /// 获取当前页数据
        /// </summary>
        /// <param name="list">总的list数据</param>
        /// <param name="jqgridparam">分页参数</param>
        /// <returns></returns>
        public List<Model.View_Order_MerSet> FindListPage(List<Model.View_Order_MerSet> list, ref JqGridParam jqgridparam)
        {
            try
            {
                List<Model.View_Order_MerSet> RetList = new List<Model.View_Order_MerSet>();
                string orderField = jqgridparam.sort;//排序列
                string orderType = jqgridparam.order;//排序方式
                int pageIndex = jqgridparam.page;//当前页
                int pageSize = jqgridparam.rows;//每页行数
                int totalRow = jqgridparam.records;//总记录数
                int lastIndex = pageIndex * pageSize;//当前页最后一行
                if (orderType == "asc")
                {
                    list = (from p in list orderby orderField ascending select p).ToList();
                }
                else if ((orderType == "desc"))
                {
                    list = (from p in list orderby orderField descending select p).ToList();
                }

                //如果总行数小于当前页最后一行时 当前页最后一行等于总行数
                if (totalRow < lastIndex)
                {
                    lastIndex = totalRow;
                }
                int fistIndex = (pageIndex - 1) * pageSize;
                int j = 0;
                for (int i = fistIndex; i < lastIndex; i++)
                {
                    j++;
                    RetList.Add(list[i]);
                }
                return RetList;
            }
            catch (Exception)
            {
                return null;
            }
        }
        #endregion

        public ActionResult Update(UpdateData data)
        {
            try
            {
                Model.View_OperUser user = (Model.View_OperUser)Session[SessionStorage.OperUser];
           
                string json = data.ToJson();
                string result = SRS.SetOrder_Key(user.OperID, json, 2, GetYZM._YZM, 1);
                JObject jb = JObject.Parse(result);
                string bs = jb["BS"].ToString();
                if (bs == "1")
                {
                    return Content(bs);
                }
                else
                {
                    return Content(jb["Msg"].ToString());
                }
            }
            catch(Exception ex)
            {
                return Content("-1");  
            
            }

        }
        public class UpdateData
        {
            public int ComOpen { get; set; }// 是否开启评论
            public int IntOpenPT { get; set; }// 是否允许平台积分消费
            public int IntOpenSH { get; set; }// 是否允许商户积分消费
            public int IntShOpen { get; set; }// 是否允许商户开启积分
            public string TopMer { get; set; }//排序的方式
        }
    }
}
